package net.zjitc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.io.ByteStreams;
import io.minio.DownloadObjectArgs;
import io.minio.MinioClient;
import io.minio.StatObjectResponse;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.zjitc.common.ResponseResult;
import net.zjitc.constant.RedisConstant;
import net.zjitc.entity.Apply;
import net.zjitc.entity.VO.ApplyVo;
import net.zjitc.service.IApplyService;
import net.zjitc.service.ICarouseService;
import net.zjitc.until.MinioUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * @auther 郭梦婷
 * @create 2022-05-09 11:22:19
 * @describe 前端控制器
 */
@CrossOrigin
@RestController
@Api(tags = "应聘信息管理")
public class ApplyController {
    @Reference
    private IApplyService iApplyService;

    ResponseResult result=new ResponseResult();

    @Value("${minio.bucketName}")
    private String bucketName;
    @Autowired
    private MinioClient minioClient;
    @Autowired
    private MinioUtils minioutils;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Reference
    private ICarouseService carouseService;

    @ApiOperation("上传简历")
    @PostMapping("apply/upload")
    public ResponseResult upload(@RequestParam("File") MultipartFile File){

        try {
            List<String> upload = minioutils.upload(new MultipartFile[]{File});
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,upload.get(0));
            result.setSuccess("文件上传成功",upload.get(0));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setError("文件上传失败");
            return result;
        }
    }
    @ApiOperation("下载简历")
    @PostMapping("apply/download")
    public ResponseEntity download(@RequestParam("resume") String resume) throws IOException, InvalidKeyException, InvalidResponseException, InsufficientDataException, NoSuchAlgorithmException, ServerException, InternalException, XmlParserException, ErrorResponseException {


        return minioutils.download(resume);

    }

    @ApiOperation("获取所有应聘信息")
    @GetMapping("GetAllapply")
    public ResponseResult getApply(@RequestParam(required = false,value ="querystring" ) String querystring,
                                      @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                      @RequestParam(required = true,value = "pagesize") Integer pagesize){
        Page<ApplyVo> apply=iApplyService.getApply(querystring,pagenum,pagesize);
        if (apply.getTotal()>0){
            result.setSuccess("获取成功",apply);
        }else {
            result.setError("获取失败");
        }
        return result;
    }
    @ApiOperation("根据id查找应聘信息")
    @GetMapping("applyById/{id}")
    public ResponseResult getApplyById(@PathVariable("id") Integer id){
        ApplyVo applyVo = iApplyService.selectById(id);
        if (applyVo!=null){
            result.setSuccess("获取用户成功",applyVo);
        }else {
            result.setError("获取用户失败");
        }
        return result;
    }

    @ApiOperation("修改应聘信息")
    @PutMapping("Updateapply/{id}")
    public ResponseResult AddApply(@RequestBody Apply apply,@PathVariable("id") int id){
        apply.setId(id);
        boolean update = iApplyService.updateById(apply);
        if (update){
            result.setSuccess("修改应聘信息成功",update);
        }else {
            result.setError("修改应聘信息失败");
        }
        return result;
    }
    @ApiOperation("删除应聘信息")
    @DeleteMapping("Deleteapply/{id}")
    public ResponseResult Deleteapply(@PathVariable("id") int id){

        int delete= iApplyService.deleteById(id);
        if (delete!=0){
            result.setSuccess("删除应聘信息");
        }else {
            result.setError("删除应聘信息");
        }
        return result;
    }
    @ApiOperation("添加应聘信息")
    @GetMapping("Addapply")
    public ResponseResult Addapply(@RequestParam(required = true) Integer recruitid,
                                   @RequestParam(required = true) String username,
                                   @RequestParam(required = true) String resume){

        int insert=iApplyService.addApply(recruitid,username,resume);
        if (insert!=0){
            result.setSuccess("投递成功");
        }else {
            result.setError("投递失败");
        }
        return result;
    }
    @ApiOperation("是否投递简历")
    @GetMapping("IfApply")
    public ResponseResult IfApply(@RequestParam(required = true) Integer recruitid,
                                   @RequestParam(required = true) String username){

        int insert=iApplyService.IfApply(recruitid,username);
        if (insert!=0){
            result.setSuccess("已应聘");
        }else {
            result.setError("未应聘");
        }
        return result;
    }
    @ApiOperation("我投递的简历")
    @GetMapping("getAllApplyByUsername")
    public ResponseResult getAllApplyByUsername(@RequestParam(required = true) String username,
                                                @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                                @RequestParam(required = true,value = "pagesize") Integer pagesize){

        Page<ApplyVo> apply=iApplyService.getAllApplyByUsername(username,pagenum,pagesize);
        if (apply.getTotal()!=0){
            result.setSuccess("获取已投简历成功",apply);
        }else {
            result.setError("获取已投简历失败");
        }
        return result;
    }

    @ApiOperation("查找应聘用户")
    @GetMapping("selectJobSeek/{id}")
    public ResponseResult selectJobSeek(@PathVariable("id") int id,
                                        @RequestParam(required = false) String querystring,
                                        @RequestParam(required = true ,value = "pagenum") Integer pagenum,
                                        @RequestParam(required = true,value = "pagesize") Integer pagesize){

        Page<ApplyVo> apply=iApplyService.selectJobSeek(id,querystring,pagenum,pagesize);
        if (apply.getTotal()!=0){
            result.setSuccess("获取已投简历成功",apply);
        }else {
            result.setError("获取已投简历失败");
        }
        return result;
    }


}

